package com.lagou.rpc.provider;

import com.lagou.rpc.provider.config.NettyConfig;
import com.lagou.rpc.provider.server.RpcServer;
import com.lagou.rpc.provider.server.ZkServer;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServerBootstrapApplication implements CommandLineRunner {


    @Autowired
    RpcServer rpcServer;

    @Autowired
    NettyConfig nettyConfig;

    @Autowired
    ZkServer zkServer;

    public static void main(String[] args) {
        SpringApplication.run(ServerBootstrapApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                rpcServer.startServer(nettyConfig.getIp(), nettyConfig.getPort());
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                ZkClient zkClient = new ZkClient("192.168.0.131:2181");
                System.out.println("服务端8898（tomcat8081,ip127.0.0.1）与zookeeper会话被创建了..");
                zkServer.setZkServer(zkClient,nettyConfig.getIp(),nettyConfig.getPort());
                System.out.println(zkServer.zkUp());
            }
        }).start();
    }
}
